<template>
	<view class="page" v-if="orderInfo">
		<view class="info">
			<text class="tips">{{remainingTime | formatTime}}</text>
			<text class="price">{{orderInfo.order_money}}</text>
		</view>

		<view class="pay-list">
			<view class="title">支付方式</view>
			<radio-group @change="radioChange" class="radio-group">
				<label class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in payTypeList" :key="item.value">
					<view class="left">
						<image :src="item.icon" mode="widthFix"></image>
						<view>
							<text>{{item.name}}</text>
						</view>
					</view>
					<view>
						<radio :value="item.value" :checked="index === current" color="#FA802A" />
					</view>
				</label>
			</radio-group>
		</view>

		<view class="btn" @tap="onsubmit" :class="{disabled:remainingTime == -1}">
			确认交易
		</view>
		<text class="order_sn" @tap="$wanlshop.copy(orderInfo.order_sn)">订单号：{{orderInfo.order_sn}}</text>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				timer: null,
				orderInfo: null,
				payTypeList: [{
						value: '1',
						name: '微信支付',
						icon: "/static/restaurant/common/wechat.png"
					},
					{
						value: 'balance',
						name: '余额支付',
						icon: "/static/restaurant/common/wallet.png"
					}
				],
				current: 1,
				order_id: '',
				targetTime: new Date(), // 指定的目标时间
				remainingTime: 0, // 剩余时间
			}
		},
		computed: {
			...mapState(['user', 'common']),
		},
		filters: {
			formatTime(value) {
				if (value === -1) {
					return '交易已过期'; // 过期处理
				} else {
					let days = Math.floor(value / (1000 * 60 * 60 * 24));
					let hours = Math.floor((value % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
					let mins = Math.floor((value % (1000 * 60 * 60)) / (1000 * 60));
					let secs = Math.floor((value % (1000 * 60)) / 1000);
					hours = hours < 10 ? '0' + hours : hours
					mins = mins < 10 ? '0' + mins : mins
					secs = secs < 10 ? '0' + secs : secs
					let str = ''
					if(days > 0) {
						str = `交易剩余时间:${days}天${hours}小时${mins}分钟${secs}秒`
					} else if(hours > 0) {
						str = `交易剩余时间:${hours}小时${mins}分钟${secs}秒`
					} else if(mins > 0) {
						str = `交易剩余时间:${mins}分钟${secs}秒`
					}
					return str;
				}
			},
		},
		onLoad(option) {
			this.order_id = option.id
			this.loadData(option.id)
		},
		onHide() {
			clearInterval(this.timer)
			this.timer = null;
		},
		onUnload() {
			clearInterval(this.timer)
			this.timer = null;
		},
		methods: {
			updateRemainingTime() {
				const currentTime = new Date();
				const diff = this.targetTime - currentTime; // 目标时间与当前时间的差值（毫秒）
				// console.log(diff)
				if (diff <= 0) {
					this.remainingTime = -1; // 过期处理
				} else {
					this.remainingTime = diff;
				}
			},
			async loadData(id) {
				let res = await this.$api.getPayInfo({
					order_id: id,
					order_type: 'restaurant'
				})
				if (res.code == 1) {
					this.targetTime = new Date(res.data.last_pay_time)
					this.orderInfo = res.data;
					this.updateRemainingTime();
					this.timer = setInterval(() => {
						this.updateRemainingTime();
					}, 1000);
				}
			},
			radioChange: function(evt) {
				for (let i = 0; i < this.payTypeList.length; i++) {
					if (this.payTypeList[i].value === evt.detail.value) {
						this.current = i;
						break;
					}
				}
			},
			async onsubmit() {
				if(this.remainingTime == -1) return this.$wanlshop.msg('交易已过期')
				let res = await this.$api.payment({
					pay_type: this.payTypeList[this.current].value,
					order_id: this.order_id
				})
				if (res.code == 1) {
					this.$wanlshop.msg('支付成功')
					setTimeout(() => {
						uni.redirectTo({
							url: '/pages/user/order/status?t=success'
						})
					}, 1500);
				}
			}
		}
	}
</script>
<style>
	page {
		background-color: #F5F5F5;
	}
</style>
<style lang="less" scoped>
	.page {
		padding-top: 168rpx;

		.info {
			display: flex;
			flex-direction: column;
			align-items: center;

			text {
				&.tips {
					color: #999999;
					font-size: 28rpx;
					margin-bottom: 18rpx;
				}

				&.price {
					color: #333333;
					font-size: 80rpx;
					font-weight: 500;
					position: relative;
					display: flex;
					justify-content: flex-start;
					align-items: center;
					padding-left: 30rpx;

					&::after {
						content: "¥";
						display: inline-block;
						font-size: 48rpx;
						left: 0;
						position: absolute;
						top: 30rpx;
					}
				}
			}
		}

		.pay-list {
			width: 690rpx;
			margin: 190rpx auto 254rpx;

			.title {
				color: #555555;
				font-size: 28rpx;
				margin-bottom: 18rpx;
			}

			.radio-group {
				width: 100%;
				background-color: #fff;
				border-radius: 16rpx;
				padding: 0 26rpx;
				box-sizing: border-box;

				.uni-list-cell-pd {
					height: 115rpx;
					display: flex;
					justify-content: space-between;
					align-items: center;

					.left {
						display: flex;
						justify-content: flex-start;
						align-items: center;

						image {
							width: 60rpx;
							height: 60rpx;
							margin-right: 26rpx;
						}

						text {
							color: #333333;
							font-size: 28rpx;
						}
					}
				}
			}
		}

		.btn {
			width: 510rpx;
			height: 84rpx;
			line-height: 84rpx;
			text-align: center;
			color: #fff;
			font-size: 34rpx;
			margin: 0 auto;
			background: #FA802A;
			border-radius: 42rpx;
			&.disabled {
				background-color: #ccc;
				color: #fff;
			}
		}

		.order_sn {
			color: #ccc;
			font-size: 28rpx;
			text-align: center;
			display: inline-block;
			width: 100%;
			margin-top: 40rpx;
		}
	}
</style>